package service;
import java.util.*;

import util.RegexUtil;
import domain.Movie;
import domain.User;

import dao.UserDAO;
import dao.MovieDAO;
import ex.DatabaseException;



public class SearchService {

	

	
	public HashMap<User,Long> searchUsers(String query) throws DatabaseException
	{
		String[] tokens = RegexUtil.chopQuery(query);
		
		HashMap<User,Long> resultMap = new HashMap<User,Long>();
		UserDAO uDao = new UserDAO();
		
		for (int i = 0; i < tokens.length; i++)
		{
			LinkedList<User> matches = uDao.searchUserByText(tokens[i]);
			for (User user : matches) {
				if(resultMap.containsKey(user))
				{
					long count = resultMap.get(user);
					resultMap.put(user, ++count);
				}
				else
				{
					resultMap.put(user, 1L);
				}
			}
		}
		
		System.out.println("service " + resultMap);
		return resultMap;
	}
	
	public HashMap<Movie,Long> searchMovies(String query) throws DatabaseException
	{
		String[] tokens = RegexUtil.chopQuery(query);
		
		HashMap<Movie,Long> resultMap = new HashMap<Movie,Long>();
		MovieDAO mDao = new MovieDAO();
		
		for (int i = 0; i < tokens.length; i++)
		{
			LinkedList<Movie> matches = mDao.searchMovieByText(tokens[i]);
			for (Movie movie : matches) {
				if(resultMap.containsKey(movie))
				{
					long count = resultMap.get(movie);
					resultMap.put(movie, ++count);
				}
				else
				{
					resultMap.put(movie, 1L);
				}
			}
		}
		
		System.out.println("service " + resultMap);
		
		return resultMap;
	}
	
}
